package cn.zyjblogs.utils.word;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import org.ddr.poi.html.HtmlRenderConfig;
import org.ddr.poi.html.HtmlRenderPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;

/**
 * html转word工具类
 *
 * @author zhuyijun
 * @version 2.0.0
 * @description
 * @create 2024/6/4 8:41
 * <author>                <time>                  <version>                   <description>
 * zhuyijun             2024/6/4 8:41                 2.0.0
 */
public class HtmlToWordConverter {
    public static final Logger log = LoggerFactory.getLogger(HtmlToWordConverter.class);

    public static void converterToFileByTemplatePath(String filePath, String html, String templatePath) {
        try (InputStream inputStream = new ByteArrayInputStream(Files.readAllBytes(Path.of(templatePath)))) {
            HtmlRenderConfig config = new HtmlRenderConfig();
            //添加头处理器
            config.setCustomRenderers(List.of(new NewHeaderRenderer()));
            // 给标签绑定插件
            Configure configure = Configure.builder().bind("content", new HtmlRenderPolicy(config)).build();
            // 给标签绑定插件
            XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(Map.of("content", html));
            // 将完成数据渲染的文档写出
            template.writeToFile(filePath);
        } catch (IOException e) {
            log.error("导出失败,e:{}", e.getMessage());
        }
    }

    public static void converterToFileByTemplate(String filePath, String html) {
        try (InputStream inputStream = HtmlToWordConverter.class.getResourceAsStream("/templates/template.docx")) {
            HtmlRenderConfig config = new HtmlRenderConfig();
            //添加头处理器
            config.setCustomRenderers(List.of(new NewHeaderRenderer()));
            // 给标签绑定插件
            Configure configure = Configure.builder().bind("content", new HtmlRenderPolicy(config)).build();
            // 给标签绑定插件
            XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(Map.of("content", html));
            // 将完成数据渲染的文档写出
            template.writeToFile(filePath);
        } catch (IOException e) {
            log.error("导出失败,e:{}", e.getMessage());
        }
    }

    public static void converterToFile(String filePath, String html) {
        try (InputStream inputStream = HtmlToWordConverter.class.getResourceAsStream("/templates/template.docx")) {
            HtmlRenderConfig config = new HtmlRenderConfig();
            //添加头处理器
            config.setCustomRenderers(List.of(new NewHeaderRenderer()));
            // 给标签绑定插件
            Configure configure = Configure.builder().bind("content", new HtmlRenderPolicy(config)).build();
            // 给标签绑定插件
            XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(Map.of("content", html));
            // 将完成数据渲染的文档写出
            template.writeToFile(filePath);
        } catch (IOException e) {
            log.error("导出失败,e:{}", e.getMessage());
        }
    }

    public static void converter(HttpServletResponse response, String html, String fileName) {
        try (InputStream inputStream = HtmlToWordConverter.class.getResourceAsStream("/templates/template_docx.docx")) {
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader(HttpHeaders.PRAGMA, "no-cache");
            response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate");
            HtmlRenderConfig config = new HtmlRenderConfig();
            //添加头处理器
            config.setCustomRenderers(List.of(new NewHeaderRenderer()));
            // 给标签绑定插件
            Configure configure = Configure.builder().bind("content", new HtmlRenderPolicy(config)).build();
            // 给标签绑定插件
            XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(Map.of("content", html));
            // 将完成数据渲染的文档写出
            template.writeAndClose(response.getOutputStream());

        } catch (IOException e) {
            log.error("导出失败,e:{}", e.getMessage());
        }
    }
}
